#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.ComponentModel;
using System.Windows.Forms;
using ZO.SmartCore.Core;

#endregion

namespace ZO.SmartCore.Windows.Forms
{
    /// <summary>
    /// Provides the base implementation for the IComponent interface and enables 
    /// object sharing between applications. 
    /// </summary>
    [ToolboxItem(false)]
    public class SmartComponent : Component
    {

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SmartComponent"/> class.
        /// </summary>
        public SmartComponent()
            : base()
        {
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="SmartComponent"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        public SmartComponent(IContainer container)
        {
            container.Add(this);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SmartComponent"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="parentContainer">The parent container.</param>
        public SmartComponent(IContainer container, ContainerControl parentContainer)
        {
            container.Add(this);
            this.ContainerControl = parentContainer;
        }

        #endregion

        #region Destructor
        
        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="SmartComponent"/> is reclaimed by garbage collection.
        /// </summary>
        ~SmartComponent()
        {
            this.Dispose(false);
        }
        #endregion

        #region Fields
        private ContainerControl _ContainerControl = null;
        /// <summary>
        /// Track whether Dispose has been called.
        /// </summary>
        private DisposeState _disposeState = DisposeState.None;


        /// <summary>
        /// Contain zero or more components. 
        /// </summary>
        protected IContainer Components = null;



        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the container control.
        /// </summary>
        /// <value>The container control.</value>
        public ContainerControl ContainerControl
        {
            get { return _ContainerControl; }
            private set { _ContainerControl = value; }
        }

        /// <summary>
        /// Get Whether Object is Disposed.
        /// </summary>
        /// <value>True If Object is disposed else false.</value>
        public bool IsDisposed
        {
            get { return (this._disposeState == DisposeState.Disposed); }

        }

        /// <summary>
        /// Get Whether Object is Disposing.
        /// </summary>
        /// <value>True If Object is Disposing.</value>
        public bool IsDisposing
        {
            get { return (this._disposeState == DisposeState.Disposing); }
        }

   
        #endregion

        #region Methods
        /// <summary>
        /// Method for checking the disposed flag and raising the 
        /// <see cref="T:System.ObjectDisposedException"/> exception
        /// </summary>
        /// <exception cref="T:System.ObjectDisposedException">
        ///		Thrown when a Object is Disposing Or Disposed
        ///	</exception>
        protected void CheckDisposed()
        {
            if (this._disposeState == DisposeState.Disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }
        }

        /// <summary>
        /// Used Internally To Dipose Object.
        /// </summary>
        /// <param name="disposing">If true, this method is called because the object is being disposed with the Dispose() method. If false, the object is being disposed by the garbage collector.</param>
        protected override void Dispose(bool disposing)
        {
            //Check to see if Dispose has already been called.
            if ((this._disposeState != DisposeState.Disposed) & (this._disposeState != DisposeState.Disposing))
            {
                //change the state to Disposing
                this._disposeState = DisposeState.Disposing;

                try
                {
                    //If disposing equals true, dispose all managed 
                    // and unmanaged resources.
                    if (disposing)
                    {
                        if (Components != null)
                        {
                            Components.Dispose();
                        }
                        this.DisposeManagedResources();
                        this.DisposeUnmanagedResources();

                    }
                    else
                    {
                        this.DisposeUnmanagedResources();
                    }

                }

                catch
                {
                    this._disposeState = DisposeState.None;
                    throw;
                }
                this._disposeState = DisposeState.Disposed;

                base.Dispose(disposing);

            }
        }


        /// <summary>
        /// Override This Method To Dispose Unmanaged Resources.
        /// </summary>
        protected virtual void DisposeManagedResources() { this.ContainerControl = null; }

        /// <summary>
        /// Override This Method To Dispose Managed Resources.
        /// </summary>
        protected virtual void DisposeUnmanagedResources() { }


    

   
        #endregion

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            Components = new System.ComponentModel.Container();
        }

        #endregion
    
    }
}
